package com.controller;


import com.alibaba.fastjson.JSON;
import com.domain.*;


import com.webservice.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin")
public class AdminController {


    @Resource
    private LoginWebservice loginWebservice;

    @Resource
    private EquipmentWebservice equipmentWebservice;

    @Resource
    private OrderWebservice orderWebservice;

    @Resource
    private UserWebservice userWebservice;

    @Resource
    private ProcessWebservice processWebservice;

    @Resource
    private StatisticsWebService statisticsWebService;

    @Autowired
    private FileService fileService;
    /**
     * 注册审核操作
     *
     * @param user_id
     * @param choice
     * @param flag
     * @return
     */
    @RequestMapping("/registerChoice")
    public String choice(int user_id, int choice, int flag) {

        if (choice == 1) {//审核通过
            userWebservice.updateAccount_state(user_id, 2);
        } else if (choice == 2) {//审核不通过，删除用户
            userWebservice.delUser(user_id);
        }
        if (flag == 1) {
            return "redirect:/admin/registerInsider";
        } else {
            return "redirect:/admin/registerOutsider";
        }
    }

    /**
     * 内部人员注册审核
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/reviewerRegister")
    public String reviewerRegister(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<User> userList = userWebservice.findUserByState(1);
        List<User> users = new ArrayList<User>();
        if (userList != null) {
            for (User user : userList) {
                if (user.getRole_id() > 0) {
                    //System.out.println("&&&*****");
                    //System.out.println(user.getInsiderUnit_name());
                    //System.out.println("&&&*****");
                    users.add(user);
                }
            }
        }

        model.addAttribute("users", users);
        return "/admin/reviewerRegister";
    }

    /**
     * *注册审核判断1
     *
     * @return
     */
    @RequestMapping("/gotos")
    public String gotos(Integer user_id) {
        //System.out.println(user_id);
        userWebservice.updateAccount_state(user_id, 2);
        return "redirect:/admin/outsideReg";

    }

    /**
     * *注册审核判断2
     *
     * @return
     */
    @RequestMapping("/reject")
    public String reject(Integer user_id, Integer choice) {
        userWebservice.delUser(user_id);
        return "redirect:/admin/outsideReg";
    }


    /**
     * 维修方人员注册管理
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/outsideReg")
    public String outsideReg(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<User> userList = userWebservice.findUserByState(1);
        List<User> users = new ArrayList<User>();
        if (userList != null) {
            for (User user : userList) {
                if (user.getRole_id() == -1 || user.getRole_id() == -2) { //-1为维修公司角色id
                    user = userWebservice.findById(user.getId());
                    users.add(user);
                }
            }
        }
        model.addAttribute("users", users);
        return "/admin/outsideReg";
    }

    /**
     * 维修方个人信息管理
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/peopleMangage")
    public String peopleMangage(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<User> userList = userWebservice.findUserByState(2);
        List<User> userList2 = userWebservice.findUserByState(1);
        int number1 = 0, number2 = 0;
        if (userList2 != null) {
            userList.addAll(userList2);
        }
        List<User> users = new ArrayList<User>();
        if (userList != null) {
            for (User user : userList) {
                if (user.getRole_id() < 0) { //-1为维修公司角色id
                    user = userWebservice.findById(user.getId());
                    users.add(user);
                    if (user.getRole_id() == -2)
                        number1++;
                    if (user.getRole_id() == -1)
                        number2++;
                }
            }
        }

        model.addAttribute("number1", number1);
        model.addAttribute("number2", number2);
        model.addAttribute("users", users);
        return "/admin/peopleMangage";
    }

    /**
     * 内部人员管理
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/insideManage")
    public String insideManage(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<User> users = userWebservice.findUserByState(2);
        List<User> userList2 = userWebservice.findUserByState(1);
        int number1 = 0, number2 = 0, number3 = 0;
        if (userList2 != null) {
            users.addAll(userList2);
        }
        List<User> users_t = new ArrayList<User>();
        if (users != null) {
            for (User s : users) {
                if (s.getRole_id() > 1) {
                    users_t.add(s);
                    if (s.getRole_id() == 2)
                        number1++;
                    if (s.getRole_id() == 3)
                        number2++;
                    if (s.getRole_id() == 4)
                        number3++;
                }
            }
        }
        model.addAttribute("number1", number1);
        model.addAttribute("number2", number2);
        model.addAttribute("number3", number3);
        model.addAttribute("users", users_t);
        return "/admin/insideManage";
    }

    /**
     * 设备管理
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/equipmentMan")
    public String equipmentMan(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        Integer num = 1;
        model.addAttribute("admin", admin);
        //通过维修单id查找维修类型的id
        List<Equipment> equipmentList1 = new ArrayList<Equipment>();
        equipmentList1 = equipmentWebservice.findAllEquipment();
        model.addAttribute("e_list", equipmentList1);
        return "/admin/equipmentMan";
    }

    /**
     * 所有已经完成订单信息
     *
     * @param time
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/ordered_details")
    public String ordered_details(String time, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        float sum = 0;
        int num = 0;
        if (time != null) {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            maintenance_orders = orderWebservice.findAllOrderByTimeUnfinished(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }

            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        } else {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            time = sdf.format(date);
            maintenance_orders = orderWebservice.findAllOrderByTimeUnfinished(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }
            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        }

        model.addAttribute("time",time);
        return "/admin/ordered_details";
    }

    /**
     * 已经完成订单信息
     *
     * @param time
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/allorderdetails")
    public String allorderdetails(String time, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        float sum = 0;
        int num = 0;
        if (time != null) {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            maintenance_orders = orderWebservice.findAllOrderByTimeFinished(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }

            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        } else {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            time = sdf.format(date);
            maintenance_orders = orderWebservice.findAllOrderByTimeFinished(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }
            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        }

        model.addAttribute("time",time);
        return "/admin/allorderdetails";
    }

    /**
     * 所有订单信息
     *
     * @param time
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/allorders")
    public String allorders(String time, Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        float sum = 0;
        int num = 0;
        if (time != null) {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            maintenance_orders = orderWebservice.findAllOrderByTime(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }

            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        } else {
            List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            time = sdf.format(date);
            maintenance_orders = orderWebservice.findAllOrderByTime(time);
            if (maintenance_orders!=null) {
                for (Maintenance_order orders : maintenance_orders) {
                    sum = sum + orders.getFinal_price();
                }
                model.addAttribute("number", maintenance_orders.size());
            }else{
                model.addAttribute("number", 0);
            }
            model.addAttribute("price", sum);
            model.addAttribute("orderlist", maintenance_orders);
        }

        model.addAttribute("time",time);
        return "/admin/allorders";
    }

    /**
     * 内部人员注册审核
     * @param user_id
     * @param choice
     * @return
     */
    @RequestMapping("/outregister")
    public String outregister(Integer user_id, Integer choice){
        if(choice==1){

            userWebservice.updateAccount_state(user_id,2);

        }else{

            userWebservice.delUser(user_id);
        }
        return "redirect:/admin/outsideReg";
    }
    /**
     * 内部人员注册审核
     * @param user_id
     * @param choice
     * @return
     */
    @RequestMapping("/registerReview")
    public String registerReview(Integer user_id, Integer choice){
        if(choice==1){

            userWebservice.updateAccount_state(user_id,2);

        }else{

            userWebservice.delUser(user_id);
        }
        return "redirect:/admin/reviewerRegister";
    }

    /**
     * 内部人员删除
     * @param user_id
     * @return
     */
    @RequestMapping("/insideDel")
    public String delpeople(Integer user_id){

        userWebservice.updateAccount_state(user_id,3);
        return "redirect:/admin/insideManage";
    }
    /**
     * 维修方人员删除
     * @param user_id
     * @return
     */
    @RequestMapping("/outsideDel")
    public String outsideDel(Integer user_id){

        userWebservice.updateAccount_state(user_id,3);
        return "redirect:/admin/peopleMangage";
    }

    /**
     * 报修单位添加页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/localAdd")
    public String localAdd(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        return "/admin/localAdd";
    }

    /**
     * 报修单位信息列表页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/repairLocal")
    public String repairLocal(Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<InsiderUnit> list = new ArrayList<>();
        list = userWebservice.findAllInsiderUnit();
        model.addAttribute("list",list);
        return "/admin/repairLocal";
    }

    /**
     * 修改报修单位页面
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/modifName")
    public String modifName(Integer id, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        model.addAttribute("id",id);
        return "/admin/modifName";
    }

    /**
     * 添加报修单位方法
     * @param map
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/add")
    public int add(@RequestBody Map<String,String> map, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        int i = userWebservice.addInsiderUnit(map.get("name"));
        return i;
    }

    /**
     * 修改报修单位方法
     * @param map
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/modif")
    public int modif(@RequestBody Map<String,String> map, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        int i = userWebservice.updateInsiderUnit(Integer.valueOf(map.get("id")),map.get("name"));
        return i;
    }

    /**
     * 报修单位删除操作
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/localDel")
    public String localDel(Integer id, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        //删除方法
        //System.out.println("%%%%%%");
        //System.out.println(id);
        int i = userWebservice.delInsiderUnit(id);
        //System.out.println(i);
        //System.out.println("%%%%%%");
        return "redirect:/admin/repairLocal";
    }

    /**
     * 设备详情列表页面
     * @param equipment_id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/equip_orderdetails")
    public String equip_orderdetails(Integer equipment_id, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User approver = (User) session.getAttribute("user");
        model.addAttribute("approver", approver);
        //System.out.println(equipment_id);

        List<Maintenance_order> maintenance_orders = new ArrayList<Maintenance_order>();
        maintenance_orders = orderWebservice.findOrderByEquipment(equipment_id);
        Equipment equipment = equipmentWebservice.findById(equipment_id);

        if(maintenance_orders!=null){
            //System.out.println(maintenance_orders.size());
        }

        //System.out.println("&&&&&&");
        model.addAttribute("orders",maintenance_orders);
        model.addAttribute("name",equipment);
        return "/admin/equip_orderdetails";
    }

    /**
     * 维修信息打印页面
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/processPrint")
    public String ProcessPrint(Integer id,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user_p = (User) session.getAttribute("user");
        model.addAttribute("user", user_p);
        Maintenance_order order = orderWebservice.findOne(id);
        model.addAttribute("order",order);
        List<Maintenance_request> equipment_adminRequest = new ArrayList<>();
        List<Maintenance_request> approverRequest = new ArrayList<>();
        List<Maintenance_process> first_reviewerAdvice = new ArrayList<>();
        List<Maintenance_process> addEvaluation = new ArrayList<>();
        List<Maintenance_process> first_reviewerAcceptAdvice = new ArrayList<>();
        List<Maintenance_request> maintenance_requestList = new ArrayList<>();
        String finishTime = "";
        maintenance_requestList = orderWebservice.findAllRequest(order.getId());
        List<Maintenance_process> maintenance_processList = orderWebservice.findProcess(order.getId());
        List<Expense_details> expense_detailsList = new ArrayList<>();
        expense_detailsList = orderWebservice.findxpenseDetailsByOrder(order.getId());
        model.addAttribute("process", maintenance_processList);
        model.addAttribute("expense_details", expense_detailsList);
        if (maintenance_requestList != null){
            for (Maintenance_request maintenance_request:maintenance_requestList){
                if (maintenance_request.getRole_id() == 2){
                    equipment_adminRequest.add(maintenance_request);
                }
                if (maintenance_request.getRole_id() == 3){
                    if(order.getEquipment_admin_name().equals(maintenance_request.getUser_name())){
                        equipment_adminRequest.add(maintenance_request);
                    }
                    else {
                        approverRequest.add(maintenance_request);
                    }

                }
            }
        }

        if (maintenance_processList!=null){
            for (Maintenance_process maintenance_process:maintenance_processList){
                if (maintenance_process.getTask_id() == 4 || maintenance_process.getTask_id() == 11){
                    first_reviewerAdvice.add(maintenance_process);
                }
//                if (maintenance_process.getTask_id() == 8){
//                    first_reviewerAcceptAdvice.add(maintenance_process);
//                }
                if (maintenance_process.getTask_id() == 10){
                    addEvaluation.add(maintenance_process);
                }
                if (maintenance_process.getTask_id() == 5){
                    finishTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(maintenance_process.getFinished_time());
                }
            }
        }

        model.addAttribute("finishTIme", finishTime);
//        model.addAttribute("first_reviewerAcceptAdvice", first_reviewerAcceptAdvice);
        model.addAttribute("equipment_adminRequest",equipment_adminRequest);
        model.addAttribute("approverRequest",approverRequest);
        model.addAttribute("first_reviewerAdvice",first_reviewerAdvice);
        model.addAttribute("addEvaluation",addEvaluation);
        return "processPrint";
    }

    /**
     * 图片附表
     * @param id
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/pricture")
    public String picture(Integer id,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        Maintenance_order maintenance_order = orderWebservice.findOne(id);
        String problem_images = maintenance_order.getProblem_image();
        String solut_images = maintenance_order.getSolution_image();
        List<String> map1 = new ArrayList<>();
        List<String> map2 = new ArrayList<>();
        if(problem_images!=null){
            String[] strings = problem_images.split(",");
            if (strings!=null){
                //System.out.println("0*******0");
                //System.out.println(problem_images);
                //System.out.println("0*******0");
                for (String str:strings){
                    if (str!=null && !str.equals("")){
                        map1.add(str);
                    }
                }
            }
            model.addAttribute("problem",map1);
        }
        if(solut_images!=null){
            String[] string = solut_images.split(",");
            if (string!=null){
            //System.out.println("1*******1");
            //System.out.println(solut_images);
            //System.out.println("1*******1");
            for (String str:string){
                if (str!=null && !str.equals("")){
                    map2.add(str);
                }
            }
        }
        model.addAttribute("solut",map2);
    }

        return "picture";
    }

    @RequestMapping("/download")
    public String  down(String filenames,HttpServletRequest request,HttpServletResponse response) throws Exception {
        //模拟文件，myfile.txt为需fil要下载的文件filenames

        String fileName = "D:/mmms-assets/" + filenames;
        //获取输入流
        //System.out.println("******文件" + filenames);
        BufferedOutputStream out = null;
        if (!filenames.equals("")) {
            try {
                String lastname = filenames.substring(filenames.lastIndexOf("."));

                InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
                //假如以中文名下载的话
                String filename = "下载文件" + lastname;
                //转码，免得文件名中文乱码
                filename = URLEncoder.encode(filename, "UTF-8");
                //设置文件下载头
                response.addHeader("Content-Disposition", "attachment;filename=" + filename);
                //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
                response.setContentType("multipart/form-data");
                out = new BufferedOutputStream(response.getOutputStream());
                int len = 0;
                while ((len = bis.read()) != -1) {
                    out.write(len);
                    out.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
            return "";
        }else{
            return "redirect:/error";
        }
    }

//    /**
//     * 删单操作
//     * @param id
//     * @param model
//     * @param request
//     * @return
//     */
//    @RequestMapping("/del_order")
//    public String del_order(Integer id, Model model, HttpServletRequest request){
//        HttpSession session = request.getSession();
//        User approver = (User) session.getAttribute("user");
//        model.addAttribute("approver", approver);
//        System.out.println(id);
//        int i = processWebservice.delOrder(id);
//        System.out.println("完成删单"+i);
//        return "redirect:/admin/allorderdetails";
//    }
    /**
     * 删单操作
     * @param map
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/del_order")
    @ResponseBody
    public int del_order(@RequestBody Map<String,String> map, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        //正确的返回值为0
        int i = processWebservice.delOrder(Integer.parseInt(map.get("id")));
        //System.out.println("删单是否成功，看i的取值！"+i);
        return i;
    }


    @RequestMapping("/info_show")
    public String info_show(String times,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        Date time = new Date();
        int n =0,m = 0,b=0 ,id;//n完成m异常b正在进行
        float company=0.0f,people=0.0f;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        //System.out.println(year);
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Integer> list3 = new ArrayList<>();
        List<Float> list4 = new ArrayList<>();
        List<Float> list5 = new ArrayList<>();
        if(times!=null) {
           year = times;
        }
        for (int i = 1; i <= 12; i++) {
            List<Maintenance_order> list = new ArrayList<>();
            list = orderWebservice.findAllOrderByTime(year + "-" + i);
                if (list != null) {
                    n = 0;
                    m = 0;
                    b = 0;
                    company = 0;
                    people = 0;
                    for (Maintenance_order maintenance_order : list) {
                        if (maintenance_order.getUnit_id() != null) {
                            id = maintenance_order.getUnit_id();
                            User user = userWebservice.findById(id);
                            if (user != null) {
                                if (user.getRole_id() == -1) {
                                    company = company + maintenance_order.getFinal_price();
                                }
                                if (user.getRole_id() == -2) {
                                    people = people + maintenance_order.getFinal_price();
                                }
                            }
                        }
                        if (maintenance_order.getOrder_state() == 10) {
                            n++;
                        } else if (maintenance_order.getOrder_state() == 11) {
                            m++;
                        } else {
                            b++;
                        }
                    }
                    list1.add(n);
                    list2.add(m);
                    list3.add(b);
                    list4.add(company);
                    list5.add(people);
                } else {
                    list1.add(0);
                    list2.add(0);
                    list3.add(0);
                    list4.add(Float.valueOf("0"));
                    list5.add(Float.valueOf("0"));
                }
            }
            model.addAttribute("list1", list1);
            model.addAttribute("list2", list2);
            model.addAttribute("list3", list3);
            model.addAttribute("list4", list4);
            model.addAttribute("list5", list5);

            List<Integer> list_1 = new ArrayList<>();
            List<Integer> list_2 = new ArrayList<>();
            List<Integer> list_3 = new ArrayList<>();
            for (int i = 1; i <= 12; i++) {
                List<Maintenance_order> maintenance_orders1 = orderWebservice.findAll(year + "-" + i, 1);//营房
                List<Maintenance_order> maintenance_orders2 = orderWebservice.findAll(year + "-" + i, 2);//装备
                List<Maintenance_order> maintenance_orders3 = orderWebservice.findAll(year + "-" + i, 3);//公共
                if (maintenance_orders1 != null) {
                    list_1.add(maintenance_orders1.size());
                } else {
                    list_1.add(0);
                }

                if (maintenance_orders2 != null) {
                    list_2.add(maintenance_orders2.size());
                } else {
                    list_2.add(0);
                }

                if (maintenance_orders3 != null) {
                    list_3.add(maintenance_orders3.size());
                } else {
                    list_3.add(0);
                }
            }
            model.addAttribute("list_1", list_1);
            model.addAttribute("list_2", list_2);
            model.addAttribute("list_3", list_3);

            model.addAttribute("year",year);
        return "/admin/infor_show";
    }

    @RequestMapping("/equipStatis")
    public String equipStatis(String times,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        Date time = new Date();
        int d1=0,d2=0,d3=0;//n完成m异常b正在进行
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        if(times!=null){
            year = times;
        }
        List<Map<String, Object>> lm = new ArrayList<>();
        List<Integer> lm2 = new ArrayList<>();
        List<Integer> lm3 = new ArrayList<>();
        List<Integer> lm4 = new ArrayList<>();
        int dates = Integer.parseInt(year);
        Map<String, Object> map = new HashMap<>();
        List<Integer> dt = new ArrayList<>();
        for(int i = dates-5;i<=dates;i++){
            map = JSON.parseObject(statisticsWebService.maintenancePriceAndTimes(String.valueOf(i)));
            //营房
            d1 = statisticsWebService.maintenanceTimesOfEquipmentYear(3,String.valueOf(i));
            //装备
            d2 = statisticsWebService.maintenanceTimesOfEquipYear(String.valueOf(i));
            //公共
            d3 = statisticsWebService.maintenanceTimesOfEquipmentYear(6,String.valueOf(i));
            lm.add(map);
            lm2.add(d1);
            lm3.add(d2);
            lm4.add(d3);
            dt.add(i);
        }
        model.addAttribute("house",lm2);
        model.addAttribute("equip",lm3);
        model.addAttribute("public1",lm4);
        model.addAttribute("dt",dt);
        model.addAttribute("list",lm);
        model.addAttribute("year",year);
        model.addAttribute("map",map);
        return "/admin/equipStatis";
    }

    @RequestMapping("/statispage")
    public String statispage(String times,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        Date time = new Date();
        int n =0,m = 0,b=0 ,id;//n完成m异常b正在进行
        float company=0.0f,people=0.0f;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        List<User> userList = userWebservice.findUserByState(2);
        int q=0,w=0,e=0,r=0,t=0;
        if(times!=null){
            year = times;
        }
        Map<String, Object> map = new HashMap<>();

        Map<String, Object> map2 = new HashMap<>();
        int dates = Integer.parseInt(year);
        List<Integer> dt = new ArrayList<>();
        List<Map<String, Object>> lm1 = new ArrayList<>();

        for(int i = dates-5;i<=dates;i++){
            //公司
            map = JSON.parseObject(statisticsWebService.maintenancePriceOfMaintenanceUnitYear(String.valueOf(i)));
            lm1.add(map);
            dt.add(i);
        }
        model.addAttribute("dt",dt);
        model.addAttribute("map",lm1);

        if (userList != null) {
            for(User user:userList){
                if(user.getRole_id()==-1){
                    q++;
                }else if(user.getRole_id()==-2){
                    w++;
                }else if(user.getRole_id()==2){
                    e++;
                }else if(user.getRole_id()==3){
                    r++;
                }else if(user.getRole_id()==4){
                    t++;
                }
            }
        }
        model.addAttribute("q",q);
        model.addAttribute("w",w);
        model.addAttribute("e",e);
        model.addAttribute("r",r);
        model.addAttribute("t",t);
        List<Float> list4 = new ArrayList<>();
        List<Float> list5 = new ArrayList<>();
        for(int i = 1;i<=12;i++) {
            List<Maintenance_order> list = new ArrayList<>();
            list = orderWebservice.findAllOrderByTime(year+"-"+i);
            if(list!=null) {
                n=0;
                m=0;
                b=0;
                company=0;
                people=0;
                for(Maintenance_order maintenance_order:list){
                    if(maintenance_order.getUnit_id()!=null) {
                        id = maintenance_order.getUnit_id();
                        User user = userWebservice.findById(id);
                        if (user != null) {
                            if (user.getRole_id() == -1) {
                                company=company+maintenance_order.getFinal_price();
                            }
                            if (user.getRole_id() == -2) {
                                people=people+maintenance_order.getFinal_price();
                            }
                        }
                    }
                    if(maintenance_order.getOrder_state()==10){
                        n++;
                    }else if(maintenance_order.getOrder_state()==11){
                        m++;
                    }else{
                        b++;
                    }
                }
                list4.add(company);
                list5.add(people);
            }else{
                list4.add(Float.valueOf("0"));
                list5.add(Float.valueOf("0"));
            }
        }
        model.addAttribute("list4",list4);
        model.addAttribute("list5",list5);
        model.addAttribute("year",year);
        return "admin/statispage";
    }
    @RequestMapping("/equips")
    @ResponseBody
    public List<String> equips(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<Equipment> list_equip = new ArrayList<>();
        list_equip  = equipmentWebservice.findAllEquipment();
        List<String> list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        for(Equipment equipment:list_equip){
            list.add("{"+"value:"+equipment.getMaintenance_times()+","+"name:"+"'"+equipment.getEquipment_name()+"'"+"}");
        }
        return list;
    }

    //设备维修费用
    @RequestMapping("/equips_fee")
    @ResponseBody
    public List<String> equips_fee(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<Equipment> list_equip = new ArrayList<>();
        list_equip  = equipmentWebservice.findAllEquipment();
        Date time = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        List<String> list = new ArrayList<>();
        float money = 0.0f,money1=0.0f,money2=0.0f;
        for(Equipment equipment:list_equip){
            //System.out.println(equipment.getEquipment_name());
            String name = equipment.getEquipment_name();
            money = statisticsWebService.maintenancePriceOfEquipmentYear(equipment.getEquipment_id(),year);
            list.add("{"+"value:"+money+","+"name:"+"'"+equipment.getEquipment_name()+"'"+"}");

        }
        money1 = statisticsWebService.maintenancePriceOfEquipmentYear(3,year);
        list.add("{"+"value:"+money1+","+"name:"+"'"+"营房"+"'"+"}");
        money2 = statisticsWebService.maintenancePriceOfEquipmentYear(6,year);
        list.add("{"+"value:"+money2+","+"name:"+"'"+"公共"+"'"+"}");
        return list;
    }

    @RequestMapping("/lstatis")
    @ResponseBody
    public List<String> lstatis(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<InsiderUnit> list_local = new ArrayList<>();
        list_local  = userWebservice.findAllInsiderUnit();
        List<String> list = new ArrayList<>();
        int m = 0;
        List<User> user = new ArrayList<>();
        for(InsiderUnit insider:list_local){
            user = userWebservice.findUserByInsiderUnit(insider.getInsiderUnit_id());
            if(user!=null){
                m = user.size();
            }else{
                m = 0;
            }
            list.add("{"+"value:"+m+","+"name:"+"'"+insider.getInsiderUnit_name()+"'"+"}");
        }
        return list;
    }
    @RequestMapping("/lstatis_fee")
    @ResponseBody
    public List<String> lstatis_fee(Integer id,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        Date time = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        List<String> list = new ArrayList<>();
        int m = 0;
        float money =0.0f;
        List<User> user = new ArrayList<>();
        int dates = Integer.parseInt(year);
        for(int i = dates-5;i<=dates;i++){
            money = statisticsWebService.maintenancePriceOfInsiderUnitYear(id,String.valueOf(i));
            list.add("{"+"value:"+money+","+"name:"+"'"+i+"'"+"}");
        }
        //System.out.println("*****pp");
        return list;
    }
    @RequestMapping("/localStatis")
    public String localStatis(Integer id,String name,HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        List<InsiderUnit> list_local = new ArrayList<>();
        List<Integer> dt = new ArrayList<>();
        List<Float> df = new ArrayList<>();
        list_local  = userWebservice.findAllInsiderUnit();
        Date time = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(time);
        String year = date.substring(0,4);
        float money = 0.0f;
        if(id==null){
            id = 4;
        }
        int dates = Integer.parseInt(year);
        for(int i = dates-5;i<=dates;i++){
            money = statisticsWebService.maintenancePriceOfInsiderUnitYear(id,String.valueOf(i));
           // money = (float)8.9+i-2000;
            df.add(money);
            dt.add(i);
        }
        model.addAttribute("dt",dt);
        model.addAttribute("df",df);
        model.addAttribute("year",year);
        model.addAttribute("list",list_local);
        model.addAttribute("name",name);
        return "admin/localStatis";
    }
    /**
     * 口令更新方法
     * @param map
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/mods")
    @ResponseBody
    public int mod(@RequestBody Map<String,String> map, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        int i = fileService.updateDownloadPassword(map.get("name"),map.get("value"));
        return i;
    }

    @RequestMapping("/modvalue")
    public String gotosmodvalue(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        model.addAttribute("admin", admin);
        return "admin/modvalue";

    }
}
